package com.zhuozhou.jdyc.main.main.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivityClassifyBinding;
import com.zhuozhou.jdyc.main.main.adapter.ClassifyAdapter;
import com.zhuozhou.jdyc.main.main.adapter.IndexMerchantAdapter;
import com.zhuozhou.jdyc.main.main.bean.ShopCommodityListBean;
import com.zhuozhou.jdyc.main.main.bean.ShopIndexBean;
import com.zhuozhou.jdyc.main.main.bean.TabItemBean;
import com.zhuozhou.jdyc.main.main.viewmodel.MainViewModel;
import com.zhuozhou.jdyc.util.apiUtil.StringUtil;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/8/17
 * Describe:分类
 */
public class ClassifyActivity extends BaseActivity<MainViewModel, ActivityClassifyBinding> implements IndexMerchantAdapter.OnMerchantItemClickListener, ClassifyAdapter.OnTabItemClickListener {

    private ClassifyAdapter classifyAdapter;
    private IndexMerchantAdapter indexMerchantAdapter;
    private List<ShopIndexBean> listShop = new ArrayList<>();
    private String tabName = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_classify;
    }

    @Override
    public String setTitleBar() {
        return "商家分类";
    }

    @Override
    public void initView() {
        setThemeBarAndToolBarColor(R.color.themeColor);
        tabName = baseBundle.getString("name");
        mViewModel.shopCommodityListMutableLiveData.observe(this, v -> {
            List<TabItemBean> listItem = new ArrayList<>();
            listItem.add(new TabItemBean(-1, "所有商家", true));
            for (ShopCommodityListBean.ArrayBean.TypeListBean item : v.getArray().getTypeList()) {
                listItem.add(new TabItemBean(item.getId(), item.getName(), false));
            }
            classifyAdapter = new ClassifyAdapter(this);
            classifyAdapter.setList(listItem);
            classifyAdapter.setOnTabItemClickListener(this);
            binding.mRecyclerViewClassify.setAdapter(classifyAdapter);
            binding.mRecyclerViewClassify.setLayoutManager(new MyLayoutManager(this));
            listShop = v.getArray().getCommodityList();
            if (StringUtil.isEmpty(baseBundle.getInt("id"))) {
                setMerchantList(v.getArray().getCommodityList());
            } else {
                setClassifyAdapter(baseBundle.getInt("id"));
            }
        });

        mViewModel.shopCommodityDetailMutableLiveData.observe(this, v -> {
            List<ShopIndexBean> list = new ArrayList<>();
            for (ShopIndexBean item : v.getArray().getCommodityList()) {
                item.setName(tabName);
                list.add(item);
            }
            setMerchantList(list);
        });
        mViewModel.basicsSearchAdminMutableLiveData.observe(this, v -> {
            setMerchantList(v.getObj());
        });
        binding.searchEdit.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.searchEdit.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                try {
                    mViewModel.getBasicsSearchAdmin(UserAccountHelper.getNewCity(), v.getText().toString(), 0);
                    setClassifyAdapter(-1);
                    // 当按了搜索之后关闭软键盘
                    ((InputMethodManager) binding.searchEdit.getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            this.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return false;
        });
        binding.tvSearch.setOnClickListener(v -> {
            mViewModel.getBasicsSearchAdmin(UserAccountHelper.getNewCity(), binding.searchEdit.getText().toString(), 0);
            setClassifyAdapter(-1);
        });
    }

    public void setClassifyAdapter(int id) {
        List<TabItemBean> list = new ArrayList<>();
        for (TabItemBean item : classifyAdapter.getList()) {
            if (item.getIsView()) {
                item.setIsView(false);
            }
            if (item.getId() == id) {
                item.setIsView(true);
            }
            list.add(item);
        }
        classifyAdapter.setList(list);
        classifyAdapter.notifyDataSetChanged();
    }

    public void setMerchantList(List<ShopIndexBean> list) {
        indexMerchantAdapter = new IndexMerchantAdapter(this);
        indexMerchantAdapter.setList(list);
        indexMerchantAdapter.setOnMerchantItemClickListener(this);
        binding.mRecyclerViewMerchant.setAdapter(indexMerchantAdapter);
        binding.mRecyclerViewMerchant.setLayoutManager(new MyLayoutManager(this));
    }

    @Override
    public void initData(Bundle bundle) {
        mViewModel.getShopCommodityList(UserAccountHelper.getNewCity());
        if (StringUtil.isNotEmpty(bundle.getInt("id")))
            mViewModel.getShopCommodityDetail(UserAccountHelper.getNewCity(), bundle.getInt("id"));
    }

    @Override
    public void onTabItemClick(View view, int position) {
        if (StringUtil.isEmpty(binding.searchEdit.getText().toString())) {
            if (classifyAdapter.getList().get(position).getId() == -1) {
                setMerchantList(listShop);
            } else {
                tabName = classifyAdapter.getList().get(position).getTopName();
                mViewModel.getShopCommodityDetail(UserAccountHelper.getNewCity(), classifyAdapter.getList().get(position).getId());
            }
        } else {
            mViewModel.getBasicsSearchAdmin(UserAccountHelper.getNewCity(), binding.searchEdit.getText().toString(), classifyAdapter.getList().get(position).getId());
        }
    }

    @Override
    public void onMerchantItemClick(View view, int position) {
        Intent in = new Intent(this, MerchantActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", indexMerchantAdapter.getList().get(position).getId() + "");
        in.putExtras(bundle);
        startActivity(in);
    }
}
